/*
 * MyProject-Gruntfile.js
 */

module.exports = function (grunt) {
    'use strict';
    var config = grunt.file.readJSON('./config.json', { encoding: 'utf8' });

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        banner: '/*!\n' +
            ' * Project v<%= pkg.version %> (<%= pkg.homepage %>)\n' +
            ' * Copyright 2011-<%= grunt.template.today("yyyy") %> <%= pkg.author.name %>\n' +
            ' * Licensed under <%= pkg.license.type %> (<%= pkg.license.url %>)\n' +
            ' */\n',

        clean: {
            dist : 'dist'
        },
        usebanner: {
            options: {
                position: 'top',
                banner: '<%= banner %>'
            },
            files: {
                src: 'dist/css/*.css'
            }
        },
        less: {
            compileCore: {
                options: {
                    strictMath: true,
                    sourceMap: true,
                    outputSourceFiles: true,
                    sourceMapURL: '<%= pkg.name %>.css.map',
                    sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
                },
                src: 'less/pro.less',
                dest: 'dist/css/<%= pkg.name %>.css'
            }
        },
        csslint: {
            dist : [
                'dist/css/<%= pkg.name %>.css'
            ]
        },
        autoprefixer: {
            options: {
                browsers: config.config.autoprefixerBrowsers
            },
            core: {
                options: {
                    map: true
                },
                src: 'dist/css/<%= pkg.name %>.css'
            }
        },
        csscomb: {
            options: {
            },
            dist: {
                expand: true,
                cwd: 'dist/css/',
                src: ['*.css', '!*.min.css'],
                dest: 'dist/css/'
            }
        },
        cssmin: {
            options: {
                // TODO: disable `zeroUnits` optimization once clean-css 3.2 is released
                //    and then simplify the fix for https://github.com/twbs/bootstrap/issues/14837 accordingly
                compatibility: 'ie8',
                keepSpecialComments: '*',
                advanced: false
            },
            minifyCore: {
                src: 'dist/css/<%= pkg.name %>.css',
                dest: 'dist/css/<%= pkg.name %>.min.css'
            }
        },
        jshint: {
            core: {
                src: 'js/*.js'
            }
        },
        concat: {
            options: {
            },
            bootstrap: {
                src: [
                    'js/accessTime.js',
                    'js/alert.js'
                ],
                dest: 'dist/js/<%= pkg.name %>.js'
            }
        },
        uglify: {
            options: {
                preserveComments: 'some'
            },
            core: {
                src: 'dist/js/*.js',
                dest: 'dist/js/<%= pkg.name %>.min.js'
            }
        },
        qunit: {
            all: ['js/tests/*.html']
        },
        watch: {
            test: {
                files: 'js/test/*.js',
                tasks: ['jshint', 'qunit']
            },
            js: {
                files: 'js/*.js',
                tasks: ['dist-js']
            },
            less: {
                files: 'less/*.less',
                tasks: ['dist-css']
            }
        },
        compress: {
            main: {
                options: {
                    archive: '<%= pkg.name %>-<%= pkg.version %>-dist.zip',
                    mode: 'zip',
                    level: 9,
                    pretty: true
                },
                files: [
                    {
                        expand: true,
                        cwd: 'dist/',
                        src: ['**'],
                        dest: '<%= pkg.name %>-<%= pkg.version %>-dist'
                    }
                ]
            }
        }
    });

    require('load-grunt-tasks')(grunt, { scope: 'devDependencies' });

    // TEST distribution task.
    grunt.registerTask('test-css', ['less:compileCore', 'csslint']);
    grunt.registerTask('test-js', function (filename) {
        if (filename) {
            grunt.config('jshint.core.src', 'js/' + filename + '.js');
            grunt.config('qunit.all', ['js/tests/' + filename + '.html']);
        }
        grunt.task.run(['jshint', 'qunit']);
    });

    // CSS distribution task.
    grunt.registerTask('less-compile', ['less:compileCore']);
    grunt.registerTask('dist-css', ['less-compile', 'autoprefixer', 'usebanner', 'csscomb', 'cssmin']);

    // JS distribution task.
    grunt.registerTask('dist-js', ['concat', 'uglify']);

    // Full distribution task.
    grunt.registerTask('dist', ['clean:dist', 'dist-css', 'dist-js']);
    grunt.registerTask('test', ['test-css', 'test-js']);
    grunt.registerTask('live', ['watch']);
    grunt.registerTask('zip', ['compress']);

    //Global distribution task.
    grunt.registerTask('default', ['test', 'dist', 'zip']);
};